package top.kpromise.user.crash;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class DingTalkMessageBuilder {

    private final HashMap<String, Object> map;

    public DingTalkMessageBuilder() {
        map = new HashMap<>();
    }

    public DingTalkMessageBuilder markdownMessage(String title, String text) {
        map.put("msgtype", "markdown");
        Map<String, String> contentMap = new HashMap<>();
        contentMap.put("title", title);
        contentMap.put("text", text);
        map.put("markdown", contentMap);
        return this;
    }

    public DingTalkMessageBuilder at(ArrayList<String> phones) {
        Map<String, Object> at = new HashMap<>();
        at.put("atMobiles", phones);
        at.put("isAtAll", false);
        map.put("at", at);
        return this;
    }

    String build() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(map);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
